<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Vote\ExportVoteSubject;
use App\Models\Vote\VoteSubject;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;

/**
 * Class DashboardController
 * @package App\Http\Controllers\Backend
 */
class DashboardController extends Controller
{
    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        return view('backend.dashboard');
    }

    /**
     * @return \Illuminate\View\View
     */
    public function vote_edit()
    {
//        Log::info('vote_edit.');
        //使用查询构建器进行简单分页，每页显示3条记录
        $votes = VoteSubject::select(
            DB::Raw('*,total/views as voterate')
        )->where("hasExported", false)->where("deleted", false)->orderBy("voterate", "desc")->paginate(10);
        return view('backend.raw-subjects', ['votes' => $votes]);
    }

    /**
     * @return \Illuminate\View\View
     */
    public function viewExportedVoteSubjects()
    {
        //使用查询构建器进行简单分页，每页显示3条记录
        $votes = ExportVoteSubject::select(
            DB::Raw('*,total/views as voterate')
        )->orderBy("voterate", "desc")->paginate(10);
        return view('backend.exported-subjects', ['votes' => $votes]);
    }

    public function updateSubject(Request $request)
    {
        $id = json_decode($request->input("id"));
        $title = $request->input("title");
        $choices = json_decode($request->input("choices"));
        if ($id != null) {
            $vote = ExportVoteSubject::where('id', $id)->first();
            if ($vote != null) {
                $vote->title = $title;
                $vote->choices = $choices;
                if($vote->save()) {
                    return response()->json(array(
                        'status' => 1,
                        'msg' => 'ok',
                    ));
                }
            }
        }
        return Redirect::back()->withInput()->withErrors('操作失败！');
    }

    /**
     * 公开待选库的投票项目.
     */
    public function exportVoteSubject(Request $request)
    {
//        Log::info($request->input("ids"));
        $ids = json_decode($request->input("ids"));
//        Log::info($ids);
        if ($ids == null) {
            return Redirect::back()->withInput()->withErrors('操作失败！');
        }
        foreach ($ids as $id) {
            $vote = VoteSubject::where('id', $id)->first();
            if ($vote != null) {
                $vote->hasExported = true;
                $vote->save();
                $exportVote = ExportVoteSubject::makeFromVoteSubject($vote);
                $exportVote->save();
            }
        }
        return response()->json(array(
            'status' => 1,
            'msg' => 'ok',
        ));
    }

    /**
     * 将投票项目放回待选库.
     */
    public function moveSubjectToBacklog(Request $request)
    {
        Log::info($request->input("ids"));
        $ids = json_decode($request->input("ids"));
        Log::info($ids);
        if ($ids == null) {
            return Redirect::back()->withInput()->withErrors('操作失败！');
        }
        foreach ($ids as $id) {
            $exportVote = ExportVoteSubject::where('id', $id)->first();
            $vote = VoteSubject::where('id', $id)->first();
            if ($vote != null) {
                $vote->hasExported = false;
                $vote->save();
                $exportVote->delete();
            }
        }
        return response()->json(array(
            'status' => 1,
            'msg' => 'ok',
        ));
    }

    /**
     * 重置投票项目数据为原始数据.
     */
    public function resetVoteSubject(Request $request)
    {
        $ids = json_decode($request->input("ids"));
        if ($ids == null) {
            return Redirect::back()->withInput()->withErrors('操作失败！');
        }
        foreach ($ids as $id) {
            $exportVote = ExportVoteSubject::where('id', $id)->first();
            $vote = VoteSubject::where('id', $id)->first();
            if ($vote != null && $exportVote != null) {
                $exportVote->copy($vote);
                $exportVote->save();
            }
        }
        return response()->json(array(
            'status' => 1,
            'msg' => 'ok',
        ));
    }

    /**
     * 移动投票项目到垃圾桶.
     */
    public function moveVoteSubjectToRecycleBin(Request $request)
    {
//        Log::info($request->input("ids"));
        $ids = json_decode($request->input("ids"));
//        Log::info($ids);
        if ($ids == null) {
            return Redirect::back()->withInput()->withErrors('删除失败！');
        }
        foreach ($ids as $id) {
            $vote = VoteSubject::where('id', $id)->first();
            if ($vote != null) {
                $vote->deleted = true;
                $vote->save();
            }
        }
        return response()->json(array(
            'status' => 1,
            'msg' => 'ok',
        ));
    }
}